* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file sets up the dataset used for the regression analysis of export market outcomes.

* From Do File #P9
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_imputation_prepare_jc_multi_d_si4_1865.dta", clear

* Merge with firm-level data
rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge 1:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keep(match master) nogen
rename aar aarA
rename aar_ori aar
rename cvrnr cvrnrA

* From Do File #P11
merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr_dist_v2.dta", keep(match master) gen(_mergedist)

* From Do File #P5
merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_nprodpermark.dta", keep(match master) nogen

rename lnnprodpermark lnanprodpermark
rename lnmaxnprodpermark lnamaxnprodpermark

gen nproddivnmark = nprod/nmark
gen lnnproddivnmark = ln(nproddivnmark)

* Drop if acquirer has no merger event in remaining sample
egen meanaar = mean(aar), by(cvrnrA)
drop if meanaar==. & acquirer==1

* Drop if acquirer has multiple merger events
egen sdaar = sd(aar), by(cvrnrA)
drop if sdaar>0 & sdaar!=.

* Calculate minimum number of employees in sample
egen minemployee_cvrnr = min(employee_cvrnr) if nprod>0 & nprod!=., by(cvrnrA)

* Calculate minimum number of products in sample
egen minnprod = min(nprod) if nprod>0 & nprod!=., by(cvrnrA)

* Additional calculations
replace d_yr = aarA - meanaar

gen p4 = (d_yr>3 & acquirer==1)
gen m4 = (d_yr<-3 & acquirer==1)

* From Do File #P5
rename aar aar_ori
rename meanaar aar
merge 1:1 cvrnrA aar aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_nprodpermark_expb4.dta", keep(match master)
egen max_merge = max(_merge), by(cvrnrA)
drop _merge

replace lnnprodpermark = lnanprodpermark if acquirer==0
replace lnmaxnprodpermark = lnamaxnprodpermark if acquirer==0

rename aar meanaar
rename aar_ori aar

* Define placebo years (for Appendix Figure A.9)
gen d_yrS = aarA - meanaar + 3
gen p4S = (d_yrS>3 & acquirer==1)
gen m4S = (d_yrS<-3 & acquirer==1)
* Indicator for each Indexvalue, except 0 (reference year)
foreach i of numlist 1(1)3{
	gen m`i'S=0
	replace m`i'S=1 if d_yrS==-`i'
	}
foreach i of numlist 1(1)3 {
	gen p`i'S=0
	replace p`i'S=1 if d_yrS==`i'
	}

qui tab aarA, gen(D_AA_)
qui tab aar, gen(D_A_)

* Define spells for imputation estimator
gsort cvrnrA -aarA
by cvrnrA: carryforward aar, gen(aar2)
sort cvrnrA aarA
by cvrnrA: carryforward aar2, gen(aar3)
replace aar3 = aar3+1

compress
save "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\regression_data_export.dta", replace
